import top.malaoshi.data.DataColumn;
import top.malaoshi.data.DataQuery;
import top.malaoshi.data.DataRow;
import top.malaoshi.data.DataTable;
import top.malaoshi.util.Out;

/**
 * 测试数据添加、遍历
 */
public class S3数据_6删除行 {

    static DataTable ready(){
        DataTable dt = new DataTable("t_user");
        Out.println("表名：",dt.getName());

        dt.columns().add("id");
        dt.columns().add("username", DataColumn.Type.TEXT);
        dt.columns().add("password", DataColumn.Type.TEXT);
        dt.columns().add("age", DataColumn.Type.INT);
        dt.columns().add("height", DataColumn.Type.DOUBLE);

        dt.rows().add(new Object[]{1,"lilei","123456",20,1.83});
        DataRow drHmm = dt.rows().add(new Object[]{2, "韩梅梅", "666", 22,1.65});

        // 创建 DataRnow
        DataRow dataRow = dt.newRow();
        dt.rows().add(dataRow);
//        通过索引设置数据
        dataRow.setValue( 0,3 );
        dataRow.setValue( 1,"lucy" );
        dataRow.setValue( 2,"333" );
        dataRow.setValue( 3,22);
        dataRow.setValue( 4,1.73 );

        // 创建 DataRnow
        DataRow dataRow2 = dt.newRow();

        drHmm.setValue("id",4);
        drHmm.setValue("username","lili");
        drHmm.setValue("password","555");
        drHmm.setValue("age",28);
        drHmm.setValue("height",1.65);

        dt.rows().add(new Object[]{5,"张三","123456",20,1.83});
        dt.rows().add(new Object[]{6,"李四","123456",30,1.83});
        dt.rows().add(new Object[]{7,"杜子腾","123456",35,1.83});
        dt.rows().add(new Object[]{8,"史珍香","123456",19,1.68});

        return dt;
    }
    // 根据行索引进行删除
    public static void main1(String[] args) {

        DataTable dt = ready();
        Out.println("删除前-----------");
        printColumns(dt);
        printDatas(dt);

        dt.rows().remove(1);
        Out.println("删除后-----------");
        printColumns(dt);
        printDatas(dt);
    }
    // 根据 DataRow进行删除
    public static void main(String[] args) {

        DataTable dt = ready();

        Out.println("删除前-----------");
        printColumns(dt);
        printDatas(dt);

        DataRow[] dt2 = dt.select("username", DataQuery.Option.eq,"李四");

        Out.println("删除后-----------");
        Out.println("",dt2);
//        printColumns(dt2);
//        printDatas(dt2);

    }
    static void printColumns(DataTable dt){
        Out.println("所有列、类型");
        for(int i = 0,size = dt.columns().count();i<size;i++){
            DataColumn item = dt.columns().get(i);
            Out.println(item.getName(),item.getType());
        }
    }

    static void printDatas(DataTable dt){
        Out.println("所有的数据：");
        for(int i = 0,size = dt.rows().count(); i<size ; i++){
            DataRow row = dt.rows().get(i);

            for(int j = 0,count = dt.columns().count();j<count;j++) {
                Out.print("", row.getValue(j),",");
            }
            Out.println();
        }
    }

}
